package com.stoneframe.filter;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;
import org.java_websocket.WebSocketImpl;

import com.stoneframe.controller.base.BaseController;
import com.stoneframe.plugin.webSocketOnline.OnlineChatServer;
import com.stoneframe.plugin.websocketInstantMsg.ChatServer;
import com.stoneframe.util.baseApp.ToolHelper;
import com.stoneframe.util.baseConfig.ConfigHelper;

public class StartFilter extends BaseController implements Filter {
	
	private final Logger logger=Logger.getLogger(StartFilter.class);

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub
		
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		this.startWebSocketInstantMsg();
		this.startWebSocketOnline();
	}
	
	/**
	 * 启动即时聊天服务
	 */
	public void startWebSocketInstantMsg(){
		WebSocketImpl.DEBUG=false;
		ChatServer s;
		try{
			String webSocketPort=ConfigHelper.getPropertiesValue("WebSocketInstantMsg.Port");
			if(ToolHelper.notEmpty(webSocketPort)){
				s=new ChatServer(Integer.parseInt(webSocketPort));
				s.start();
				logger.info("websocket服务器启动,端口" + s.getPort());
			}else{
				System.out.println( "websocket服务器启动,端口未配置");
				logger.error("websocket服务器启动,端口未配置");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 启动在线服务
	 */
	public void startWebSocketOnline(){
		WebSocketImpl.DEBUG=false;
		OnlineChatServer s;
		try{
			String webSocketPort=ConfigHelper.getPropertiesValue("WebSocketOnline.Port");
			if(ToolHelper.notEmpty(webSocketPort)){
				s=new OnlineChatServer(Integer.parseInt(webSocketPort));
				s.start();
				logger.info("websocket在线服务启动,端口" + s.getPort());
			}else{
				System.out.println( "websocket在线服务启动,端口未配置");
				logger.error("websocket在线服务启动,端口未配置");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//计时器
	public void timer() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 9); // 控制时
		calendar.set(Calendar.MINUTE, 0); 		// 控制分
		calendar.set(Calendar.SECOND, 0); 		// 控制秒
		Date time = calendar.getTime(); 		// 得出执行任务的时间
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			public void run() {
				
				//PersonService personService = (PersonService)ApplicationContext.getBean("personService");

				
				//System.out.println("-------设定要指定任务--------");
			}
		}, time, 1000*60*60*24);// 这里设定将延时每天固定执行
	}

}
